package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.AmortMethodReq;
import com.jrx.anytxn.param.dto.product.AmortMethodRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IAmortMethodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AmortMethodController
 * @Description: 还款方式
 * @Author yf
 * @Date 2019/7/29 0029
 * @Version V1.0
 **/
@Api(tags = "与页面相关的还款方式表API接口")
@RestController
@RequestMapping("/param/amortMethods")
public class AmortMethodController {

    private static final Logger logger = LoggerFactory.getLogger(AmortMethodController.class);

    @Autowired
    private IAmortMethodService iAmortMethodService;
    @Autowired
    private InfoService infoService;


    @ApiOperation(value = "根据租户Id分页查询还款方式表信息", notes = "根据租户Id分页查询还款方式表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<AmortMethodRes>> getAllAmortMethodByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<AmortMethodRes>> result = new TxnRespResult<>();
        TxnPage<AmortMethodRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iAmortMethodService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据还款方式表ID和机构号查询此信息", notes = "根据还款方式表ID和机构号查询此信息")
    @GetMapping(value = "/amortMethodId/{amortMethodId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "amortMethodId", value = "还款方式ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)})
    public TxnRespResult<AmortMethodRes> getByAmortMethodIdAndOrgId(@PathVariable String amortMethodId, @PathVariable String tenantId) {
        TxnRespResult<AmortMethodRes> result = new TxnRespResult<>();
        AmortMethodRes res;
        try {
            res = iAmortMethodService.findByAmortIdIdAndTenId(amortMethodId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户I多喝还款方式ID编辑还款方式信息", notes = "根据租户I多喝还款方式ID编辑还款方式信息")
    @PutMapping("/")
    public TxnRespResult<AmortMethodRes> modifyAmortMethod(@RequestBody AmortMethodReq amortMethodReq) {
        TxnRespResult<AmortMethodRes> result = new TxnRespResult<>();
        AmortMethodRes response;
        amortMethodReq.setUpdateBy(infoService.getUser().getUserName());
        response = iAmortMethodService.modifyAmortMethod(amortMethodReq);
        result.getSuccess(response);
        return result;
    }

    @ApiOperation("添加还款方式表信息")
    @PostMapping("/")
    public TxnRespResult<AmortMethodRes> createAmortMethod(@RequestBody AmortMethodReq amortMethodReq) {
        TxnRespResult<AmortMethodRes> result = new TxnRespResult<>();
        AmortMethodRes amortMethodRes;
        try {
            amortMethodReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            amortMethodReq.setUpdateBy(infoService.getUser().getUserName());
            amortMethodRes = iAmortMethodService.addAmortMethod(amortMethodReq);
            result.getSuccess(amortMethodRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation("根据租户ID和还款方式ID删除此信息")
    @DeleteMapping("/{tenantId}/{organizationId}/{chargeOption}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "organizationId", value = "机构ID", required = true),
            @ApiImplicitParam(name = "chargeOption", value = "还款方式", required = true)
    })
    public TxnRespResult<Boolean> removeById(@PathVariable String tenantId, @PathVariable String organizationId,
                                             @PathVariable String chargeOption) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iAmortMethodService.removeByTenIdAndAmortMethodId(tenantId, organizationId,chargeOption);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "根据机构号和租户ID查询还款方式表信息", notes = "根据机构号和租户ID查询还款方式表信息")
    @GetMapping(value = "/organizationId/{organizationId}")
    @ApiImplicitParam(name = "organizationId", value = "机构号", required = true)
    public TxnRespResult<ArrayList<AmortMethodRes>> getAmortMethodByOrgId(@PathVariable String organizationId) {
        TxnRespResult<ArrayList<AmortMethodRes>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<AmortMethodRes> amortMethodResList = iAmortMethodService.getAmortMethodByOrgIdAndTenId(organizationId, tenantId);
        return result.getSuccess((ArrayList) amortMethodResList);
    }
}
